#import "CodeViewController.h"
#import "TPPasswordTextView.h"
#import "LoginUtils.h"
#import "PerfectInformationCtr.h"

@interface CodeViewController ()

//短信验证码已发送至
@property (weak, nonatomic) IBOutlet UILabel *getCodeTelephone;

//获取验证码
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;

//多少秒后重新获取
@property (weak, nonatomic) IBOutlet UILabel *reacquireLab;

//密码框背景view
@property (weak, nonatomic) IBOutlet UIView *passwordBgView;

@property (nonatomic, copy) NSString *code;

@end

@implementation CodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.tag = 10086;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setPasswordTextView];
    [self getCodeData];
    
    self.getCodeTelephone.text = [NSString stringWithFormat:@"短信验证码已发送至%@",self.telephone];
}


#pragma mark - 获取验证码
- (void)getCodeData{
    WeakSelf(self);
    [self.getCodeBtn SG_countdownWithSecond:60 completion:^{
        StrongSelf(weakSelf);
        strongSelf.reacquireLab.hidden = YES;
    }];
    
    //获取验证码
    NSString *apiStr;
    NSString *token;
    if (self.wechatLogin) {
        apiStr = API_LOGIN_bindPhoneSms;
        token = [AccountManager shareAccount].loginModel.token;
    } else {
        apiStr = API_LOGIN_sms;
        token = @"";
    }
    
    NSDictionary *param = @{
        @"phone": self.telephone,
        @"token": token
    };
    
    [[HttpHelp shareInstance] POSTHTTPV2:apiStr parameters:param success:^(NSDictionary * _Nonnull dict) {
        NSInteger result = [dict[@"result"] integerValue];
        if (result == 1) {
            self.code = dict[@"data"][@"code"];
            [self login];
        } else {
            [self mbShowToast:dict[@"msg"]];
        }
    } failure:^(BOOL netErr, NSString * _Nonnull error) {
        
    }];
}


#pragma mark - 登录
- (void)login{
    //loginType登录类型, 1-手机号码/2-微信
    NSMutableDictionary *parameter;
    if (self.wechatLogin) {
        NSString *avatarUrl = [AccountManager shareAccount].loginModel.avatarUrl;
        NSString *nickname = [AccountManager shareAccount].loginModel.nickname;
        NSString *openId = [AccountManager shareAccount].loginModel.openId;
        parameter = @{
            @"avatarUrl":avatarUrl,
            @"code":self.code,
            @"loginType":@"2",
            @"nickname":nickname,
            @"openId":openId,
            @"phone":self.telephone
        }.mutableCopy;
    } else {
        parameter = @{
            @"avatarUrl":@"",
            @"code":self.code,
            @"loginType":@"1",
            @"nickname":@"",
            @"openId":@"",
            @"phone":self.telephone
        }.mutableCopy;
    }
    
    [LoginUtils userLoginWithParam:parameter block:^(LoginModel * _Nonnull model) {
        if (nil != model) {
            [[AccountManager shareAccount] login:model];
        }
    } failure:^(NSString * _Nonnull error) {
        
    }];
}


- (void)setPasswordTextView{
    TPPasswordTextView *passwordView = [[TPPasswordTextView alloc] initWithFrame:CGRectMake(-20, 0, self.passwordBgView.width, self.passwordBgView.height)];
    passwordView.elementCount = 4;
    passwordView.elementMargin = 4;
    [self.passwordBgView addSubview:passwordView];
    
    passwordView.passwordDidChangeBlock = ^(NSString *password) {
        if ([password isEqualToString:self.code]) {
            if (self.wechatLogin) {
                [self wechatBindPhone];
            }
            
            if ([[AccountManager shareAccount].loginModel.isNewUser integerValue] == 1) {
                // newUser  0-表示老用户/1-表示新用户
                PerfectInformationCtr *ctr = [[PerfectInformationCtr alloc] init];
                ctr.telephone = self.telephone;
                [self.navigationController pushViewController:ctr animated:YES];
            } else{
                [APPDELEGATE configTab];
            }
        } else {
            if (password.length > 3) {
                [self mbShowToast:@"验证码输入有误"];
            }
        }
    };
    
    [passwordView showKeyboard];
}


#pragma mark - 微信首次登录绑定手机
- (void)wechatBindPhone{
    NSString *token = [AccountManager shareAccount].loginModel.token;
    [[HttpHelp shareInstance] POSTHTTPV2:API_LOGIN_bindPhone parameters:@{@"phone": self.telephone, @"token":token, @"code":self.code} success:^(NSDictionary * _Nonnull dict) {
        NSInteger result = [dict[@"result"] integerValue];
        if (result == 1) {
            [self mbShowToast:@"绑定成功"];
        }
        else{
            [self mbShowToast:dict[@"msg"]];
        }
    } failure:^(BOOL netErr, NSString * _Nonnull error) {
        
    }];
}


- (IBAction)popClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 重新获取
- (IBAction)getCodeClick:(UIButton *)sender {
    self.reacquireLab.hidden = NO;
    
    [self getCodeData];
}

@end
